package gov.pbc.gx.nn.pz.dao;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.bstek.bdf2.core.orm.hibernate.ISessionCallback;

import gov.pbc.gx.nn.pz.entity.HcTzckywTzsqd;
import gov.pbc.gx.nn.pz.entity.HcZzpz;
import gov.pbc.gx.nn.pz.entity.HcpzXY;
import gov.pbc.gx.nn.pz.entity.PzFont;

/**
 * 
 * @author jack.zhao 本类主要操作数据库CURD，以及其他的操作！
 */
@Repository
public class ZzpzDao extends BaseImageDao {
	public ZzpzDao() {

	}

	private static final Integer PZ_CODE_ = 2;

	/*
	 * 判断数据库中是否有相同的值
	 */
	public boolean isSameValue(HcZzpz hcZjpz) {
		String type = hcZjpz.getType();
		String pzTemplate = hcZjpz.getPzTemplate();
		String hql = " from HcZzpz where deptId=:deptId and type=:type and pzTemplate=:pzTemplate and invalid=false";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptId", this.getDeptId());
		map.put("type", type);
		map.put("pzTemplate", pzTemplate);
		List<HcZzpz> hcXjpzdatas = this.query(hql, map);
		if (!hcXjpzdatas.isEmpty()) {
			return true;
		}
		return false;
	}

	/*
	 * 保存实体
	 */
	public void saveZzpz(HcZzpz hcZjpz) {
		if (!isSameValue(hcZjpz)) {
			hcZjpz.setId(null);
			hcZjpz.setDeptId(this.getDeptId());
			this.save(hcZjpz);
		} else {
			this.update(hcZjpz);
		}
	}

	/*
	 * 根据type，pztemplate得到实体
	 */
	public HcZzpz selectOneByTypeAndTemplate(String type, String pztemplate) {
		List<HcZzpz> hcZzpzs = null;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptid", this.getDeptId());
		map.put("pztemplate", pztemplate);
		if (type == null) {
			String hql = " from HcZzpz where deptId=:deptid and type is null and pzTemplate=:pztemplate and invalid=false";
			hcZzpzs = this.query(hql, map);
		} else {
			String hql = " from HcZzpz where deptId=:deptid and type=:type and pzTemplate=:pztemplate and invalid=false";
			map.put("type", type);
			hcZzpzs = this.query(hql, map);
		}
		if (!hcZzpzs.isEmpty()) {
			return hcZzpzs.get(0);
		}
		return null;
	}

	/*
	 * 根据type得到实体的集合；
	 */
	public Collection<HcZzpz> getCoByType(String type) {
		Collection<HcZzpz> hcZzpzs;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptid", this.getDeptId());
		if (type == null) {
			String hql = " from HcZzpz where deptId=:deptid and  invalid = false and type is null";
			hcZzpzs = this.query(hql, map);
		} else {
			String hql = " from HcZzpz where deptId=:deptid and invalid = false and type=:type";
			map.put("type", type);
			hcZzpzs = this.query(hql, map);
		}
		return hcZzpzs;
	}

	/*
	 * 没有参数，得到实体按照type排序的集合
	 */
	public Collection<HcZzpz> getOneGroupByType() {
		String hql = " from HcZzpz where deptId=:deptid and invalid = false group by type";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("deptid", this.getDeptId());
		Collection<HcZzpz> hcZzpzs = this.query(hql, param);
		return hcZzpzs;
	}

	/*
	 * 以deptid为参数，得到实体集合，并根据pztemplate排序；
	 */
	public Collection<HcZzpz> getMuByDeptName() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptid", this.getDeptId());
		String hql = "from HcZzpz where deptId=:deptid and invalid = false order by pzTemplate ";
		return this.query(hql, map);
	}

	public void deleteTemplateById(String id) {
		// String hql = "update HcXjpz where id=" + id;
		// HcXjpz entity = new HcXjpz();
		// entity.setId(id);
		this.delete(id, HcZzpz.class);
	}

	// copy直接使用不需要修改！使用的是共用的类，所以不需要修改！

	// 使用父类的方法，传code，得到xy的坐标
	public List<HcpzXY> getHcPzXYs(Integer pzcode) {
		List<HcpzXY> lists = getHcPzXYsBase(pzcode);
		return lists;
	}

	// 使用父类的方法，传code，保存Hcpzyz，保存坐标
	public void saveXjpzXY(Collection<HcpzXY> hcxjpzxys) {
		saveXjpzXYBase(hcxjpzxys, PZ_CODE_);
	}

	// 使用父类的方法，传code，font，并且更新方法
	public void updateFont(PzFont pzfont) {
		updateFontBase(pzfont, PZ_CODE_);
	}

	public boolean updateTemplateNameDao(final String templatename, final String id) {

		// 得到旧的type
		String sql1 = "from HcZzpz where id=:id and invalid = false ";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("id", id);
		List<HcZzpz> HcZzpzs1 = query(sql1, param);
		String typestr = HcZzpzs1.get(0).getType();

		String sql = "from HcZzpz where  invalid = false and type=:type ";
		Map<String, Object> param1 = new HashMap<String, Object>();
		param1.put("type", typestr);

		Collection<HcZzpz> HcZzpzs = this.query(sql, param1);
		for (HcZzpz zzpz : HcZzpzs) {
			if (zzpz.getPzTemplate().equals(templatename)) {
				return false;
			}
		}
		return this.doInHibernateSession(new ISessionCallback<Boolean>() {
			String hql = "update HcZzpz set pzTemplate=:pzTemplate  where id=:id ";

			@Override
			public Boolean doInSession(Session session) {
				Query query = session.createQuery(hql);
				query.setString("pzTemplate", templatename);
				query.setString("id", id);
				query.executeUpdate();
				return true;
			}

		});

	}

	public boolean updateTemplateTypeDao(final String templatetype, final String id) {
//		String sql = "from HcZzpz where id=:id and invalid = false ";
//		Map<String, Object> param = new HashMap<String, Object>();
//		param.put("id", id);
//		Collection<HcZzpz> HcZzpzs = this.query(sql, param);
//		for (HcZzpz zzpz : HcZzpzs) {
//			if (zzpz.getType().equals(templatetype)) {
//				return false;
//			}
//		}
		
		String sql = "select count(*) from HcZzpz where type=:type and invalid = false ";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("type", templatetype);
		int count = this.queryForInt(sql, param);
		if (count > 0) {
			return false;
		}
		return this.doInHibernateSession(new ISessionCallback<Boolean>() {
			@Override
			public Boolean doInSession(Session session) {

				String sql = "from HcZzpz where id=:id and invalid = false ";
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("id", id);
				List<HcZzpz> HcZzpzs = query(sql, param);
				String typestr = HcZzpzs.get(0).getType();

				String sql1 = "from HcZzpz where type=:type and invalid = false ";
				Map<String, Object> param1 = new HashMap<String, Object>();
				param1.put("type", typestr);
				List<HcZzpz> HcZzpzs1 = query(sql1, param1);
				for (HcZzpz zzpz : HcZzpzs1) {
					String hql2 = "update HcZzpz set type=:type  where id=:id ";
					Query query = session.createQuery(hql2);
					query.setString("type", templatetype);
					query.setString("id", zzpz.getId());
					query.executeUpdate();
				}

				return true;
			}

		});

	}
}
